package io.xdag.block.model;

import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;

import org.apache.commons.lang3.ArrayUtils;

import io.xdag.io.Struct;

public class XdagBlock extends Struct implements Serializable {

	private static final long serialVersionUID = 1L;
	
	public XdagField[] fields = array(new XdagField[16]);

	private ByteBuffer bb;
	
	public XdagBlock() {};
	
	public XdagBlock(Byte[] bytes) {
		this.setByteBuffer(bytes);
	}
	
	@Override
	public boolean isPacked() {
		return true;//对齐问题
	}
	
	/**
	 * 初始化对象buffer
	 * @param bytes
	 */
	public void setByteBuffer(Byte[] bytes) {
		byte[] bs = ArrayUtils.toPrimitive(bytes);
		bb = ByteBuffer.wrap(bs);
		bb.order(ByteOrder.LITTLE_ENDIAN);
		this.setByteBuffer(bb, 0);
	}
	
	/**
	 * 初始化对象buffer
	 * @param bytes
	 */
	public void setByteBuffer(byte[] bytes) {
		bb = ByteBuffer.wrap(bytes);
		bb.order(ByteOrder.LITTLE_ENDIAN);
		this.setByteBuffer(bb, 0);
	}
	
	@Override
	public ByteOrder byteOrder() {
		return ByteOrder.LITTLE_ENDIAN;
	}
	
	public XdagField[] getFields() {
		return fields;
	}

	public void setFields(XdagField[] fields) {
		this.fields = fields;
	}
	@Override
	public String toString() {
		return "XdagBlock [fields=" + Arrays.toString(fields) + "]";
	}

	
}
